package com.jiudaoliu.homeprototype.service.impl;

import com.jiudaoliu.homeprototype.POJO.VO.CommonVO;
import com.jiudaoliu.homeprototype.entity.ModelEntity;
import com.jiudaoliu.homeprototype.repository.ModelRepository;
import com.jiudaoliu.homeprototype.service.ModelService;
import com.jiudaoliu.homeprototype.utils.CommonVO_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2018/9/20 11:12
 */
@Service
@Transactional
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelRepository modelRepository;

    @Override
    public CommonVO findAllByStatus(String status) {
        List<ModelEntity> list = modelRepository.findByStatus(status);
        TreeMap<Integer, ModelEntity> treeMap = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        if (null != list && list.size() > 0) {
            for (ModelEntity modelEntity : list) {
                treeMap.put(modelEntity.getOrders(), modelEntity);
            }
            return CommonVO_Utils.success(treeMap);
        }

        return CommonVO_Utils.fail_404("模块数据异常");
    }

    @Override
    public CommonVO findAll() {

        List<ModelEntity> list = modelRepository.findAll();
        TreeMap<Integer, ModelEntity> treeMap = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        if (null != list && list.size() > 0) {
            for (ModelEntity modelEntity : list) {
                treeMap.put(modelEntity.getOrders(), modelEntity);
            }
            return CommonVO_Utils.success(treeMap);
        }

        return CommonVO_Utils.fail_404("模块数据异常");
    }

    @Override
    public CommonVO findById(Long id) {
        Optional<ModelEntity> optional = modelRepository.findById(id);
        if (optional.isPresent()) {
            return CommonVO_Utils.success(optional.get());
        }
        return CommonVO_Utils.fail_404();
    }
}
